package com.example.brushtesting;

import android.app.Activity;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class BrushTester extends Activity implements OnItemSelectedListener, TextWatcher {
	
	private LineDrawingView ldv;
	private EditText editTextRadius;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ArrayAdapter<CharSequence> adapter;

		//Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.brush_tester_layout);
		ldv = (LineDrawingView)findViewById(R.id.lineDrawingView1);
		editTextRadius=(EditText)findViewById(R.id.edittext_radius);
		editTextRadius.addTextChangedListener(this);

		Spinner spinnerColour = (Spinner)findViewById(R.id.spinner_colour);
		spinnerColour.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner layout
		//adapter = ArrayAdapter.createFromResource(this, R.array.colours_array_items, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		//spinnerColour.setAdapter(adapter);

		Spinner spinnerStyle = (Spinner)findViewById(R.id.spinner_style);
		spinnerStyle.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner layout
		adapter = ArrayAdapter.createFromResource(this, R.array.style_array_items, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerStyle.setAdapter(adapter);

		Spinner spinnerCap = (Spinner)findViewById(R.id.spinner_cap);
		spinnerCap.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner layout
		adapter = ArrayAdapter.createFromResource(this, R.array.cap_array_items, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerCap.setAdapter(adapter);

		Spinner spinnerJoin = (Spinner)findViewById(R.id.spinner_join);
		spinnerJoin.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner layout
		adapter = ArrayAdapter.createFromResource(this, R.array.join_array_items, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinnerJoin.setAdapter(adapter);
		
		Spinner spinnerWidth = (Spinner)findViewById(R.id.spinner_width);
		spinnerWidth.setOnItemSelectedListener(this);
		
		Spinner spinnerEffect = (Spinner)findViewById(R.id.spinner_effect);
		spinnerEffect.setOnItemSelectedListener(this);
		
		Spinner spinnerBlurStyle = (Spinner)findViewById(R.id.spinner_blurstyle);
		spinnerBlurStyle.setOnItemSelectedListener(this);

	}

	public void buttonClick(View view){

		switch(view.getId()){
		case R.id.button_new:
			ldv.onClickNew(null);
			break;
		}
	}
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

		Spinner spinner = (Spinner) parent;
		switch(spinner.getId()){
		case R.id.spinner_cap:
			String cap = parent.getItemAtPosition(pos).toString().trim();
			Toast.makeText(this, cap, Toast.LENGTH_SHORT).show();
			if (cap.equalsIgnoreCase("Butt")){
				ldv.setStrokeCap(Paint.Cap.BUTT);
			}else if (cap.equalsIgnoreCase("Square")){
				ldv.setStrokeCap(Paint.Cap.SQUARE);
			}else{
				ldv.setStrokeCap(Paint.Cap.ROUND);
			}
			ldv.setPaint();
			break;
		case R.id.spinner_colour:
			String colour = parent.getItemAtPosition(pos).toString().trim();
			Toast.makeText(this, colour, Toast.LENGTH_SHORT).show();
			if (colour.equalsIgnoreCase("red")){
				ldv.setLineColour(Color.RED);
			}else if (colour.equalsIgnoreCase("green")){
				ldv.setLineColour(Color.GREEN);
			}else if (colour.equalsIgnoreCase("yellow")){
				ldv.setLineColour(Color.YELLOW);
			}else if (colour.equalsIgnoreCase("blue")){
				ldv.setLineColour(Color.BLUE);
			}else{
				ldv.setLineColour(Color.BLACK);
			}
			ldv.setPaint();
			break;
		case R.id.spinner_join:
			String join = parent.getItemAtPosition(pos).toString().trim();
			Toast.makeText(this, join, Toast.LENGTH_SHORT).show();
			if (join.equalsIgnoreCase("BEVEL")){
				ldv.setStrokeJoin(Paint.Join.BEVEL);
			}else if (join.equalsIgnoreCase("MITER")){
				ldv.setStrokeJoin(Paint.Join.MITER);
			}else{
				ldv.setStrokeJoin(Paint.Join.ROUND);
			}
			ldv.setPaint();
			break;
		case R.id.spinner_style:
			String style = parent.getItemAtPosition(pos).toString().trim();
			Toast.makeText(this, style, Toast.LENGTH_SHORT).show();
			if (style.equalsIgnoreCase("FILL")){
				ldv.setStyle(Paint.Style.FILL);
			}else if (style.equalsIgnoreCase("Fill and Stroke")){
				ldv.setStyle(Paint.Style.FILL_AND_STROKE);
			}else{
				ldv.setStyle(Paint.Style.STROKE);
			}
			ldv.setPaint();
			break;
		case R.id.spinner_effect:
			String effect = parent.getItemAtPosition(pos).toString().trim();
			
			if (effect.equalsIgnoreCase("Blur")){
				ldv.setEffect(LineDrawingView.EFFECT_BLUR);
			}else if (effect.equalsIgnoreCase("Emboss")){
				ldv.setEffect(LineDrawingView.EFFECT_EMBOSS);
			}else if (effect.equalsIgnoreCase("Shadow")){
				ldv.setEffect(LineDrawingView.EFFECT_SHADOW);
			}else if (effect.equalsIgnoreCase("ComposePathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_ComposePathEffect);
			}else if (effect.equalsIgnoreCase("CornerPathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_CornerPathEffect);
			}else if (effect.equalsIgnoreCase("DashPathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_DASH);
			}else if (effect.equalsIgnoreCase("DiscretePathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_DiscretePathEffect);
			}else if (effect.equalsIgnoreCase("PathDashPathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_PathDashPathEffect);
			}else if (effect.equalsIgnoreCase("SumPathEffect")){
				ldv.setEffect(LineDrawingView.EFFECT_SumPathEffect);
			}else{
				ldv.setEffect(LineDrawingView.EFFECT_NONE);
			}
			ldv.setPaint();
			
			break;
		case R.id.spinner_width:
			String w = parent.getItemAtPosition(pos).toString().trim();
			int width = Integer.parseInt(w);
			ldv.setLineWidth(width);
			ldv.setPaint();
			break;
		case R.id.spinner_blurstyle:
			String blurStyle = parent.getItemAtPosition(pos).toString().trim();
			if (blurStyle.equalsIgnoreCase("INNER")){
				ldv.setBlurStyle(BlurMaskFilter.Blur.INNER);
			}else if (blurStyle.equalsIgnoreCase("OUTER")){
				ldv.setBlurStyle(BlurMaskFilter.Blur.OUTER);
			}else if (blurStyle.equalsIgnoreCase("SOLID")){
				ldv.setBlurStyle(BlurMaskFilter.Blur.SOLID);
			}else{
				ldv.setBlurStyle(BlurMaskFilter.Blur.NORMAL);
			}
			ldv.setPaint();
			break;
			
		}
		
	}

	public void onNothingSelected(AdapterView<?> parent) {
		//Do nothing
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		return super.onOptionsItemSelected(item);	
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		if(!editTextRadius.getText().toString().equals("")){
			ldv.setRadius(Float.parseFloat(editTextRadius.getText().toString())); 
			ldv.setPaint();
		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}



}
